package com.fingard.dsp.bank.directbank.cmb06;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.directbank.cmb06.interfacelist.CMB06TransCode;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;

import java.io.File;

/**
 * CMB06-中国招商银行信用卡外联平台批量代收付
 */
public class CMB06BatPayRec extends CMB06Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        if ("198801".equalsIgnoreCase(p_req1988.reqHeader.commandCode)) {
            return deal_198801_CISPRQA3(p_req1988);
        } else if ("198802".equalsIgnoreCase(p_req1988.reqHeader.commandCode)) {
            return deal_198802_CISPRQA5(p_req1988);
        } else if ("198803".equalsIgnoreCase(p_req1988.reqHeader.commandCode)) {
            return deal_198803_CISPRQA7(p_req1988);
        } else if ("198804".equalsIgnoreCase(p_req1988.reqHeader.commandCode)) {
            return deal_198804_CISPRQA4(p_req1988);
        } else {
            RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
            retAts.reqSeqID = p_req1988.reqBatchNO;
            retAts.respCode = "0";
            retAts.respInfo = "不支持的指令:"+p_req1988.reqHeader.commandCode;
            retAts.transState = PaymentState.FAIL_Batch;
            retAts.payInfoCode = FailCode.E9981;
            retAts.writeRetToATS();
            return retAts;
        }
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        if ("918801".equalsIgnoreCase(p_req9188.reqHeader.commandCode)) {
            return deal_918801_CISPRQA1(p_req9188);
        } else if ("918802".equalsIgnoreCase(p_req9188.reqHeader.commandCode)) {
            return deal_918802_CISPRQA2(p_req9188);
        } else if ("918803".equalsIgnoreCase(p_req9188.reqHeader.commandCode)) {
            return deal_918803_CISPRQA7(p_req9188);
        } else {
            RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req9188.reqHeader);
            retAts.reqSeqID = p_req9188.reqBatchNO;
            retAts.respCode = "0";
            retAts.respInfo = "不支持的指令:"+p_req9188.reqHeader.commandCode;
            retAts.transState = PaymentState.FAIL_Batch;
            retAts.payInfoCode = FailCode.E9981;
            retAts.writeRetToATS();
            return retAts;
        }
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return d_QryBatPayRec(p_req9198);
    }

    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        retAts.reqSeqID = p_reqQryBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqQryBat.ownAct, "");
            if (actItem != null) {
                JSONObject jsonHeadRequest = new JSONObject();
                jsonHeadRequest.put("xWkeCod",CMB06TransCode.deal_1998_9198_CISPRQA0X0);

                JSONObject jsonBodyRequest = new JSONObject();
                JSONObject jsonCISPRQA0X0 = new JSONObject();
                jsonCISPRQA0X0.put("xTrnDbn","00");
                jsonCISPRQA0X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA0X0);

                jsonBodyRequest.put("CISPRQA0X0", jsonCISPRQA1X0Array);

                // CISPRQA0X1
                JSONObject jsonCISPRQA0X1 = new JSONObject();
                jsonCISPRQA0X1.put("xOrdNbr",p_reqQryBat.orderNo);//订单号
                jsonCISPRQA0X1.put("xTxnCtf",p_reqQryBat.reqBatchNO);//原交易流水号
                jsonCISPRQA0X1.put("xRsvFld","");//预留字段

                JSONArray jsonCISPRQA0X1Array = new JSONArray();
                jsonCISPRQA0X1Array.add(jsonCISPRQA0X1);
                jsonBodyRequest.put("CISPRQA0X1", jsonCISPRQA0X1Array);

                JSONObject jsonRequest = new JSONObject();
                jsonRequest.put("HEAD", jsonHeadRequest);
                jsonRequest.put("BODY", jsonBodyRequest);

                String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

                if (tmpStrRet[0].length() == 0) {
                    JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                    JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                    JSONArray jsonCISPRQA0Z1 = tmpRespBodyJson.getJSONArray("CISPRQA0Z1");
                    retAts.respCode = "0";
                    RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                    // 存批次号和流水号的映射
                    String tmpLocalMarkPath = getTempFileDirectory(p_reqQryBat.getBgnDateTime()) + File.separator + p_reqQryBat.reqBatchNO+".txt";
                    WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                    String postScript = FileHelper.readFileText(tmpLocalMarkPath, getCharset());
                    WriteBankLogLn2("批次文件流水号内容: " + postScript);
                    tmpRetDetail.postScript = postScript;

                    JSONObject jsonResult = jsonCISPRQA0Z1.getJSONObject(0);
                    tmpRetDetail.result = jsonResult.getString("zPcsSta") + "+" + jsonResult.getString("zFnlRes");
                    tmpRetDetail.payInfoCode = jsonResult.getString("zRspCod");
                    tmpRetDetail.memo = getBankConfig().getMapValueByKey("respInfoMapping", jsonResult.getString("zRspCod"));
                    retAts.writeRetDetail(tmpRetDetail);
                } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpStrRet[1];
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = FailCode.E9981;
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                    if (tmpStrRet[1].contains("原交易不存在")) {
                        if (canBeNotExist(p_reqQryBat, retAts, 45)) {
                            retAts.respCode = "0";
                            retAts.transState = "E1302+"+tmpStrRet[1];
                            retAts.payInfoCode = tmpStrRet[1];
                        }
                    }
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号:[" + p_reqQryBat.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeAfterDetail();
        return retAts;
    }

    public RetAtsBatPayRec deal_198801_CISPRQA3(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_190801_198801_CISPRQA3);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA1X0 = new JSONObject();
                jsonCISPRQA1X0.put("xTrnDbn","00");
                jsonCISPRQA1X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA1X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA1X0);

                jsonBodyRequest.put("CISPRQA3X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","02");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xSubOrd",p_reqBat.readedDetail.subOrderFlag);
                jsonCISPRQA1X1.put("xOrdSeq",p_reqBat.readedDetail.orderSeq);
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA3X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA3Z1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec deal_198802_CISPRQA5(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_190802_198802_CISPRQA5);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA2X0 = new JSONObject();
                jsonCISPRQA2X0.put("xTrnDbn","00");
                jsonCISPRQA2X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA2X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA2X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA2X0);

                jsonBodyRequest.put("CISPRQA5X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","06");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xSubOrd",p_reqBat.readedDetail.subOrderFlag);
                jsonCISPRQA1X1.put("xOrdSeq",p_reqBat.readedDetail.orderSeq);
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA5X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA5X1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec deal_198803_CISPRQA7(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_190803_198803_918803_CISPRQA7);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA2X0 = new JSONObject();
                jsonCISPRQA2X0.put("xTrnDbn","00");
                jsonCISPRQA2X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA2X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA2X0.put("xReqTyp","12");
                jsonCISPRQA2X0.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA2X0.put("xMerMem","");
                jsonCISPRQA2X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA2X0);

                jsonBodyRequest.put("CISPRQA7X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","12");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xPlnChg",p_reqBat.readedDetail.planChgFlag);
                jsonCISPRQA1X1.put("xPlnBef",p_reqBat.readedDetail.planBefore);
                jsonCISPRQA1X1.put("xPlnAft",p_reqBat.readedDetail.planAfter);
                jsonCISPRQA1X1.put("xPfqChg",p_reqBat.readedDetail.pfqChgFlag);
                jsonCISPRQA1X1.put("xPfqBef",getBankConfig().getMapValueByKey("xPfqMapping", p_reqBat.readedDetail.pfqBefore));
                jsonCISPRQA1X1.put("xPfqAft",getBankConfig().getMapValueByKey("xPfqMapping", p_reqBat.readedDetail.pfqAfter));
                jsonCISPRQA1X1.put("xBdtChg",p_reqBat.readedDetail.bdtChgFlag);
                jsonCISPRQA1X1.put("xBdtBef",p_reqBat.readedDetail.bdtBefore);
                jsonCISPRQA1X1.put("xBdtAft",p_reqBat.readedDetail.bdtAfter);
                jsonCISPRQA1X1.put("xSexChg",p_reqBat.readedDetail.sexChgFlag);
                jsonCISPRQA1X1.put("xSexBef",getBankConfig().getMapValueByKey("xSexMapping", p_reqBat.readedDetail.sexBefore));
                jsonCISPRQA1X1.put("xSexAft",getBankConfig().getMapValueByKey("xSexMapping", p_reqBat.readedDetail.sexAfter));
                jsonCISPRQA1X1.put("xFeeCha",p_reqBat.readedDetail.feeChaFlag);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xSubOrd",p_reqBat.readedDetail.subOrderFlag);
                jsonCISPRQA1X1.put("xOrdSeq",p_reqBat.readedDetail.orderSeq);
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA7X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA7Z1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec deal_198804_CISPRQA4(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_190804_198804_CISPRQA4);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA2X0 = new JSONObject();
                jsonCISPRQA2X0.put("xTrnDbn","00");
                jsonCISPRQA2X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA2X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA2X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA2X0);

                jsonBodyRequest.put("CISPRQA4X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","10");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xSubOrd",p_reqBat.readedDetail.subOrderFlag);
                jsonCISPRQA1X1.put("xOrdSeq",p_reqBat.readedDetail.orderSeq);
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA4X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA4Z1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec deal_918801_CISPRQA1(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_918801_CISPRQA1);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA1X0 = new JSONObject();
                jsonCISPRQA1X0.put("xTrnDbn","00");
                jsonCISPRQA1X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA1X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA1X0);

                jsonBodyRequest.put("CISPRQA1X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","00");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xInsFlg",p_reqBat.readedDetail.insFlag);
                jsonCISPRQA1X1.put("xInsCod",p_reqBat.readedDetail.sourceNoteCode);
                jsonCISPRQA1X1.put("xAddFlg",p_reqBat.readedDetail.addFlag);
                jsonCISPRQA1X1.put("xAddNum",p_reqBat.readedDetail.addNum);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA1X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA1Z1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec deal_918802_CISPRQA2(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_918802_CISPRQA2);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA2X0 = new JSONObject();
                jsonCISPRQA2X0.put("xTrnDbn","00");
                jsonCISPRQA2X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA2X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA2X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA2X0);

                jsonBodyRequest.put("CISPRQA2X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","04");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xInsFlg",p_reqBat.readedDetail.insFlag);
                jsonCISPRQA1X1.put("xInsCod",p_reqBat.readedDetail.sourceNoteCode);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA2X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA2Z1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec deal_918803_CISPRQA7(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqBat.ownAct, "");
            if (actItem == null) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号:[" + p_reqBat.ownAct + "]商户配置信息!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            JSONObject jsonHeadRequest = new JSONObject();
            jsonHeadRequest.put("xWkeCod", CMB06TransCode.deal_190803_198803_918803_CISPRQA7);

            JSONObject jsonBodyRequest = new JSONObject();
            while(p_reqBat.readRecord()) {
                JSONObject jsonCISPRQA2X0 = new JSONObject();
                jsonCISPRQA2X0.put("xTrnDbn","00");
                jsonCISPRQA2X0.put("xTxnCtf",p_reqBat.reqBatchNO);
                jsonCISPRQA2X0.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA2X0.put("xReqTyp","11");
                jsonCISPRQA2X0.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA2X0.put("xMerMem","");
                jsonCISPRQA2X0.put("xRsvFld","");

                JSONArray jsonCISPRQA1X0Array = new JSONArray();
                jsonCISPRQA1X0Array.add(jsonCISPRQA2X0);

                jsonBodyRequest.put("CISPRQA7X0", jsonCISPRQA1X0Array);

                JSONObject jsonCISPRQA1X1 = new JSONObject();
                jsonCISPRQA1X1.put("xReqTyp","11");
                jsonCISPRQA1X1.put("xMerCod",actItem.merchantId);
                jsonCISPRQA1X1.put("xOrdNbr",p_reqBat.readedDetail.orderNo);
                jsonCISPRQA1X1.put("xReqAmt",p_reqBat.readedDetail.payAmount);
                jsonCISPRQA1X1.put("xMerMem",p_reqBat.readedDetail.askNumber);
                jsonCISPRQA1X1.put("xEffDte",p_reqBat.readedDetail.effectDate);
                jsonCISPRQA1X1.put("xBegDte",p_reqBat.readedDetail.beginDate);
                jsonCISPRQA1X1.put("xEndDte",p_reqBat.readedDetail.endDate);
                jsonCISPRQA1X1.put("xPlnChg",p_reqBat.readedDetail.planChgFlag);
                jsonCISPRQA1X1.put("xPlnBef",p_reqBat.readedDetail.planBefore);
                jsonCISPRQA1X1.put("xPlnAft",p_reqBat.readedDetail.planAfter);
                jsonCISPRQA1X1.put("xPfqChg",p_reqBat.readedDetail.pfqChgFlag);
                jsonCISPRQA1X1.put("xPfqBef",getBankConfig().getMapValueByKey("xPfqMapping", p_reqBat.readedDetail.pfqBefore));
                jsonCISPRQA1X1.put("xPfqAft",getBankConfig().getMapValueByKey("xPfqMapping", p_reqBat.readedDetail.pfqAfter));
                jsonCISPRQA1X1.put("xBdtChg",p_reqBat.readedDetail.bdtChgFlag);
                jsonCISPRQA1X1.put("xBdtBef",p_reqBat.readedDetail.bdtBefore);
                jsonCISPRQA1X1.put("xBdtAft",p_reqBat.readedDetail.bdtAfter);
                jsonCISPRQA1X1.put("xSexChg",p_reqBat.readedDetail.sexChgFlag);
                jsonCISPRQA1X1.put("xSexBef",getBankConfig().getMapValueByKey("xSexMapping", p_reqBat.readedDetail.sexBefore));
                jsonCISPRQA1X1.put("xSexAft",getBankConfig().getMapValueByKey("xSexMapping", p_reqBat.readedDetail.sexAfter));
                jsonCISPRQA1X1.put("xFeeCha",p_reqBat.readedDetail.feeChaFlag);
                jsonCISPRQA1X1.put("xRsvFld","");
                jsonCISPRQA1X1.put("xSubOrd",p_reqBat.readedDetail.subOrderFlag);
                jsonCISPRQA1X1.put("xOrdSeq",p_reqBat.readedDetail.orderSeq);
                jsonCISPRQA1X1.put("xFleIdx","");

                JSONArray jsonCISPRQA1X1Array = new JSONArray();
                jsonCISPRQA1X1Array.add(jsonCISPRQA1X1);
                jsonBodyRequest.put("CISPRQA7X1", jsonCISPRQA1X1Array);

                // 存批次号和流水号的映射
                String tmpLocalMarkPath = getTempFileDirectory(p_reqBat.getPayDateTime()) + File.separator + p_reqBat.reqBatchNO+".txt";
                WriteBankLogLn2("批次文件流水号映射文件保存本机路径: " + tmpLocalMarkPath);
                FileHelper.writeFile(tmpLocalMarkPath, p_reqBat.readedDetail.askNumber, getCharset());
            }

            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("HEAD", jsonHeadRequest);
            jsonRequest.put("BODY", jsonBodyRequest);

            String[] tmpStrRet = sendToBank(jsonRequest, actItem, CMB06TransCode.deal_918801_CISPRQA1);

            if (tmpStrRet[0].length() == 0) {
                JSONObject tmpRespJson = JSONObject.parseObject(tmpStrRet[1]);
                JSONObject tmpRespBodyJson = tmpRespJson.getJSONObject("BODY");
                JSONArray jsonCISPRQA1Z1 = tmpRespBodyJson.getJSONArray("CISPRQA7Z1");
                String tmpRespCode = jsonCISPRQA1Z1.getJSONObject(0).getString("zRtnCod");
                retAts.respCode = "0";
                retAts.transState = tmpRespCode;
                retAts.payInfoCode = tmpRespCode;
                retAts.respInfo = getBankConfig().getMapValueByKey("respInfoMapping", tmpRespCode);
            } else if (tmpStrRet[0].equals(ConnState.FAIL) || tmpStrRet[0].equals(FailCode.E9981)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }
}